package org.luu.medicare.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.luu.medicare.common.Result;
import org.luu.medicare.entity.PageBean;
import org.luu.medicare.entity.TServiceFacility;
import org.luu.medicare.repository.TServiceFacilityRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ServiceFacilityService {
    private final TServiceFacilityRepo serviceFacilityRepo;

    public Result<String> deleteById(Integer id){
        Optional<TServiceFacility> facility = serviceFacilityRepo.findById(id);
        if (facility.isPresent()){
            serviceFacilityRepo.deleteById(id);
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }

    public Result<TServiceFacility> findById(Integer id){
        Optional<TServiceFacility> facility = serviceFacilityRepo.findById(id);
        return facility.map(Result::success).orElseGet(() -> Result.error("查询失败"));
    }

    public Result<PageBean<TServiceFacility>> page(int page, int pageSize, String name, String id){
        Pageable pageable = PageRequest.of(page - 1, pageSize);
        // 创建Specification用于动态查询
        Specification<TServiceFacility> spec = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (name != null && !name.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("serName"), "%" + name + "%"));
            }
            if (id != null && !id.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("serId"), "%" + id + "%"));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };
        // 执行分页查询
        Page<TServiceFacility> result = serviceFacilityRepo.findAll(spec, pageable);
        // 转换结果为PageBean
        PageBean<TServiceFacility> pageBean = new PageBean<>(result.getTotalElements(), result.getContent());
        // 日志记录
        log.info("查询服务设施，{},{}, {}", pageBean.getTotal(), result.getTotalElements(), result);
        return Result.success(pageBean);
    }

    public Result<String> save(TServiceFacility serviceFacility) {
        serviceFacilityRepo.save(serviceFacility);
        return Result.success();
    }

    public Result<String> update(TServiceFacility serviceFacility) {
        Optional<TServiceFacility> facility = serviceFacilityRepo.findById(serviceFacility.getId());
        if (facility.isPresent()){
            serviceFacilityRepo.save(serviceFacility);
        }
        return Result.success();
    }
}
